package com.gmlive.gradle

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task

class ReinforcePlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        println('[AiJiaMi] ===== Reinforce Plugin =====')

        project.extensions.create('reinforceConfig', ReinforceExtension)

        project.afterEvaluate {
            def android = project.extensions.android

            android.applicationVariants.all { variant ->
                def variantName = variant.name.capitalize()
                createTask(project, variantName)
            }

            android.buildTypes.all { buildType ->
                def buildTypeName = buildType.name.capitalize()
                createTask(project, buildTypeName)
            }

            android.productFlavors.all { flavor ->
                def flavorName = flavor.name.capitalize()
                createTask(project, flavorName)
            }
        }
    }

    private static void createTask(Project project, variantName) {
        String taskName = "reinforce${variantName}";
        boolean hasResguard = project.plugins.hasPlugin("AndResGuard")

        if (project.tasks.findByPath(taskName) == null) {
            Task task = project.task(taskName, type: ReinforceTask);
            if (hasResguard) {
                task.dependsOn "resguard${variantName}"
            } else {
                task.dependsOn "assemble${variantName}"
            }
        }
    }
}